cmake_minimum_required(VERSION 3.16)
project(Modbus_PlexLink VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# Find required Qt6 components
find_package(Qt6 REQUIRED COMPONENTS Core Widgets Network Concurrent)

message(STATUS "Qt6 Concurrent found: ${Qt6Concurrent_FOUND}")

# Set third party directory
set(THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty)

# Configure libmodbus
if(WIN32)
    # Windows: Use ThirdParty directory
    set(MODBUS_INCLUDE_DIR ${THIRD_PARTY_DIR}/libmodbus)
    set(MODBUS_LIBRARY ${THIRD_PARTY_DIR}/libmodbus/modbus.lib)
    set(MODBUS_DLL ${THIRD_PARTY_DIR}/libmodbus/modbus.dll)
    
    if(NOT EXISTS ${MODBUS_INCLUDE_DIR}/modbus.h)
        message(FATAL_ERROR "libmodbus header not found at: ${MODBUS_INCLUDE_DIR}")
    endif()
    
    if(NOT EXISTS ${MODBUS_LIBRARY})
        message(FATAL_ERROR "libmodbus library not found at: ${MODBUS_LIBRARY}")
    endif()
    
    message(STATUS "Using libmodbus from: ${THIRD_PARTY_DIR}/libmodbus")
else()
    # Linux/Unix: Try to find system libmodbus
    find_path(MODBUS_INCLUDE_DIR modbus.h
        HINTS /usr/include/modbus /usr/local/include/modbus
        PATH_SUFFIXES modbus
    )
    
    find_library(MODBUS_LIBRARY
        NAMES modbus
        HINTS /usr/lib /usr/local/lib
    )
    
    if(NOT MODBUS_INCLUDE_DIR OR NOT MODBUS_LIBRARY)
        message(FATAL_ERROR "libmodbus not found! Please install libmodbus-dev")
    endif()
endif()

# Configure libcurl (optional)
set(CURL_FOUND FALSE)
if(WIN32)
    set(CURL_INCLUDE_DIR ${THIRD_PARTY_DIR}/libcurl/include)
    set(CURL_LIBRARY ${THIRD_PARTY_DIR}/libcurl/libcurl_64bit.lib)
    
    if(EXISTS ${CURL_INCLUDE_DIR}/curl/curl.h AND EXISTS ${CURL_LIBRARY})
        set(CURL_FOUND TRUE)
        message(STATUS "Using libcurl from: ${THIRD_PARTY_DIR}/libcurl")
    else()
        message(STATUS "libcurl not found in ThirdParty directory (optional)")
    endif()
else()
    # Linux/Unix: Try to find system libcurl
    find_package(CURL QUIET)
    if(CURL_FOUND)
        message(STATUS "Using system libcurl")
    endif()
endif()

# Include directories
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/src/core
    ${CMAKE_CURRENT_SOURCE_DIR}/src/adapters
    ${CMAKE_CURRENT_SOURCE_DIR}/src/gui
    ${CMAKE_CURRENT_SOURCE_DIR}/src/utils
    ${MODBUS_INCLUDE_DIR}
)

# Add libcurl include directory if found
if(CURL_FOUND)
    include_directories(${CURL_INCLUDE_DIR})
    add_definitions(-DHAS_CURL_SUPPORT)
endif()

# Collect source files
file(GLOB_RECURSE SOURCES
    src/*.cpp
    src/*.h
)

file(GLOB_RECURSE UI_FILES
    src/gui/*.ui
)

# Create executable
add_executable(${PROJECT_NAME}
    ${SOURCES}
    ${UI_FILES}
)

# Link libraries
target_link_libraries(${PROJECT_NAME}
    Qt6::Core
    Qt6::Widgets
    Qt6::Network
    Qt6::Concurrent
    ${MODBUS_LIBRARY}
)

# Link libcurl if found
if(CURL_FOUND)
    target_link_libraries(${PROJECT_NAME} ${CURL_LIBRARY})
endif()

# Link Windows socket library
if(WIN32)
    target_link_libraries(${PROJECT_NAME} ws2_32)
endif()

# Windows specific settings
if(WIN32)
    set_target_properties(${PROJECT_NAME} PROPERTIES
        WIN32_EXECUTABLE TRUE
    )
    
    # Copy Qt DLLs to output directory
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:Qt6::Core>
        $<TARGET_FILE:Qt6::Widgets>
        $<TARGET_FILE:Qt6::Network>
        $<TARGET_FILE:Qt6::Concurrent>
        $<TARGET_FILE_DIR:${PROJECT_NAME}>
    )
    
    # Copy modbus.dll to output directory
    if(EXISTS ${MODBUS_DLL})
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${MODBUS_DLL}
            $<TARGET_FILE_DIR:${PROJECT_NAME}>
        )
        message(STATUS "Will copy modbus.dll from: ${MODBUS_DLL}")
    endif()
    
    # Copy libcurl DLL if needed (Note: libcurl_64bit.lib might be static lib)
    # If you have a libcurl.dll, add the copy command here
endif()

# Enable testing
enable_testing()
add_subdirectory(tests)
